using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class JobSeekers_personal : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            connectionString =
           WebConfigurationManager.ConnectionStrings[
           "RecruitVietnam"].ConnectionString;
            if (Session.Contents["jsuserid"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {            
                
                if(!IsPostBack)
                {
                    LoadDataForDropDownList();          
                    EditPersonalInfo();
                }
                 
            }
        }
        catch(Exception ex)
        {
            literalError.Text = ex.Message;
        }
    }
    void LoadDataForDropDownList()
    {
        DropDownListHelper helper =
                new DropDownListHelper();
        string selectQuery = 
        "Select * from tblProvinces;" +
        "Select * from tblRaces";
        Common cls = new Common(connectionString);
        DataSet dataSet = cls.GetDataSet(selectQuery );
        DropDownList[] dropDownLists = new
         DropDownList[2] { ddlProvince, ddlRace };
        helper.FillDataTableToDropDownList(
        dataSet, dropDownLists,
        new string[2] { "ProvinceName", "RaceName" },
        new string[2] { "ProvinceID", "RaceID" },
        new string[2] { "", "" }); 
    }
    
    void EditPersonalInfo()
    {
        SqlServerProvider cls = 
        new SqlServerProvider();
        cls.ConnectionString = connectionString;
        cls.CommandText = 
        "select * from vwJSPersonalInfo" +
        " where UserID=@UserID";
        cls.ParameterCollection = 
        new string[1]{"@UserID"};
        cls.ValueCollection = new 
        string[1]{Session["jsuserid"].ToString()};
        DataRow dataRow =
        cls.GetDataRow( );
        if (dataRow[1].ToString() == "")
            btnNext.Text = "Save";
        else
        {
            btnNext.Text = "Update";
            txtPermanent.Text = dataRow[1].ToString();
            txtResident.Text = dataRow[2].ToString();
            txtCity.Text = dataRow[3].ToString();
            txtDOB.Text = dataRow[4].ToString();
            rblGender.SelectedValue = 
            (dataRow[5].ToString() == "True") ? "1" : "0";
            rblMaritalStatus.SelectedValue = dataRow[6].ToString();
            ddlCountry.SelectedValue = dataRow[7].ToString();
            ddlProvince.SelectedValue = dataRow[8].ToString();
            ddlRace.SelectedValue = dataRow[9].ToString();
            txtTel.Text = dataRow[10].ToString();
            txtMobile.Text = dataRow[11].ToString();
        }
        
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        try
        {
            RecruitVietnam cls = new RecruitVietnam();
            cls.ConnectionString = connectionString;
            if(!cls.UpdatePersonal(Convert.ToString(
                Session.Contents["jsuserid"]),
                txtPermanent.Text,
                txtResident.Text,
                txtTel.Text,
                txtMobile.Text, Convert.ToInt16(
                ddlProvince.SelectedValue),
                txtCity.Text,txtDOB.Text,
                ((rblGender.SelectedItem.Value 
                == "0")?false:true), Convert.ToInt16(
                rblMaritalStatus.SelectedItem.Value),
                Convert.ToInt16(ddlRace.SelectedValue))) 
                literalError.Text="Error: "+ cls.ErrorMessage;  
            else   
                if(btnNext.Text == "Update")
                    Response.Redirect("educations.aspx");
                else  
                    Response.Redirect("education.aspx");
        }
        catch(Exception ex)
        {
            literalError.Text =ex.Message;
        }
    }
}
